using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Modules.Abstracts;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Interfaces;

namespace FiltronASP.Modules
{
    public class countryNewsModule : AbstractModule
    {
        private string currentCountryName = String.Empty;

        public override string GenerateContent(FiltronASP.Models.Page page) {
            this.currentCountryName = page.page_name;
            string htmlToReturn = String.Empty;
            IPageDAO pageDAO = new PageDAO();
            IVariableValueDAO variableValueDAO = new VariableValueDAO();
            IGalleryImageDAO galleryImageDAO = new GalleryImageDAO();
            
            IQueryable<Page> newsCollection = pageDAO.FindByLanguageAndTemplateNameAndPublished(Convert.ToInt32(page.language_id), "singleCountryNews", true);
            if (newsCollection != null) {
                bool title = false;
                

                foreach (Page singleNewsPage in newsCollection) {
                    VariableValue countriesVisibility = variableValueDAO.FindByVariableAndPage("countryNewsContent", singleNewsPage.page_id, Convert.ToInt32(page.language_id));
                    VariableValue shortNewsContent = variableValueDAO.FindByVariableAndPage("newsContentShort", singleNewsPage.page_id, Convert.ToInt32(page.language_id));
                    IQueryable<GalleryImage> newsPhoto = galleryImageDAO.FindByGalleryNameAndPage("newsPhoto", singleNewsPage.page_id);
                    string photoLink = String.Empty;
                    if (newsPhoto != null) {
                        photoLink = newsPhoto.First().gallery_image_link;
                    }
                    
                    if (countriesVisibility.variable_value.Contains(this.currentCountryName)) {
                        if (!title) {
                            htmlToReturn += String.Format("<h3 class=\"newsHeader\">{0}</h3>", Helpers.Translations.translationHelper.getTranslation("_newsTitle", Convert.ToInt32(page.language_id)));
                            title = true;
                        }
                        htmlToReturn += String.Format("<div class=\"countryNewsSingle\">");
                        htmlToReturn += String.Format(" <span class=\"countryNewsSingleTopName\">{0}</span>", singleNewsPage.page_name);
                        htmlToReturn += String.Format(" <div class=\"countryNewsContainer\">");
                        htmlToReturn += String.Format("     <div class=\"countryNewsPhoto\"><img style=\"max-width: 120px; float: left;\" src=\"{0}\" alt=\"{1}\" /></div>", photoLink, String.Empty);
                        htmlToReturn += String.Format("     <div class=\"countryNewsContent\">{0} <a href=\"{2}\">{1}</a></div>", shortNewsContent.variable_value, Helpers.Translations.translationHelper.getTranslation("_seeMore", Convert.ToInt32(page.language_id)), singleNewsPage.page_link);
                        htmlToReturn += String.Format(" </div>");
                        htmlToReturn += String.Format("</div>");
                    }
                }
            }
            return htmlToReturn;
        }
    }
}
